//
//  LGGroupMemberController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/2/24.
//  分组成员

#import "LGGroupMemberController.h"
//添加、删除患者
#import "LGMemberManageController.h"
//患者详情 - 癌症（圆荷健康）
#import "LGPatientDetailController_YHJK.h"
#import "LGPatientDetailController2.h"
#import "LGGroupMemberCell.h"
#import "LGPatientModel.h"
#define lineHeight  viewPix(90)

@interface LGGroupMemberController ()<UICollectionViewDelegate,UICollectionViewDataSource>
/**   */
@property (nonatomic , strong)UIScrollView *scrollView;
/**   */
@property (nonatomic , strong)UICollectionView *collectionView;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
@end

@implementation LGGroupMemberController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.collectionView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/patient.group/patientList" parameters:@{@"group_id":self.groupid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.dataArray = [LGPatientModel mj_objectArrayWithKeyValuesArray:returnData];
            [self.collectionView reloadData];
        }
    } error:^(NSError *error) {
        
    }];
}

-(void)setTitleStr:(NSString *)titleStr{
    _titleStr = titleStr;
    self.title = titleStr;
}

-(void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    NSInteger line = (dataArray.count+2)/5;
    line = ((dataArray.count+2)%5)>0?line+1:line;
    self.title = [NSString stringWithFormat:@"%@（%ld）",self.titleStr,dataArray.count];
    self.scrollView.frame = CGRectMake(viewPix(16), viewPix(16), Screen_W-viewPix(32), lineHeight*line+viewPix(16));
    self.scrollView.contentSize = CGSizeMake(Screen_W, lineHeight*line+viewPix(16));
    self.collectionView.frame = CGRectMake(viewPix(7), viewPix(16), Screen_W-viewPix(45), lineHeight*line);
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count+2;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LGGroupMemberCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    if (indexPath.item == 0) {
        cell.headPic.image = [UIImage imageNamed:@"团队详情-添加"];
        cell.titleLabel.text = @"添加成员";
        
    }else if (indexPath.item == 1){
        cell.headPic.image = [UIImage imageNamed:@"团队详情-删除"];
        cell.titleLabel.text = @"移除成员";
    }else{
        LGPatientModel *model = self.dataArray[indexPath.item-2];
        UIImage *placeHolder = [model.gender isEqualToString:@"女"]?defaultPatientWomen:defaultPatientMan;
        [cell.headPic sd_setImageWithURL:[NSURL URLWithString:model.avatar_url] placeholderImage:placeHolder];
        cell.titleLabel.text = model.realname;
    }
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.item < 2){
        //添加、移除成员
        LGMemberManageController *controller = [[LGMemberManageController alloc]init];
        controller.groupid = self.groupid;
        controller.isAdd = (indexPath.item == 0)?YES:NO;
        [self.navigationController pushViewController:controller animated:YES];
        
    }else{
        //癌症-患者详情
        LGPatientModel *model = self.dataArray[indexPath.item-2];
        if ([model.nodepage isEqualToString:@"1"]) {
            //市一慢阻肺
            LGPatientDetailController2 *controller = [[LGPatientDetailController2 alloc]init];
            controller.patientid = LGNSString(model.patient_id);
            controller.diseaseCode = LGNSString(model.disease_code);
            controller.levelid = LGNSString(model.level_id);
            [self.navigationController pushViewController:controller animated:YES];
            
        }else{
            //圆荷
            LGPatientDetailController_YHJK *controller = [[LGPatientDetailController_YHJK alloc]init];
            controller.patientid = LGNSString(model.patient_id);
            controller.diseaseCode = LGNSString(model.disease_code);
            controller.levelid = LGNSString(model.level_id);
            [self.navigationController pushViewController:controller animated:YES];
        }
    }
}

-(UIScrollView *)scrollView{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(viewPix(16), viewPix(16), Screen_W-viewPix(32), lineHeight)];
        [_scrollView setShadoWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:15];
        _scrollView.backgroundColor = [UIColor whiteColor];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.contentSize = CGSizeMake(Screen_W, lineHeight);
    }
    return _scrollView;
}

-(UICollectionView *)collectionView{
    if(!_collectionView){
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake((Screen_W-viewPix(45)-1)/5.0, lineHeight);
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(viewPix(7), viewPix(16), Screen_W-viewPix(45), lineHeight) collectionViewLayout:layout];
        [_collectionView registerClass:[LGGroupMemberCell class] forCellWithReuseIdentifier:@"cell"];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        
    }
    return _collectionView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
